/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package entidade.controller.JPA;


import entidade.controller.JPA.exceptions.NonexistentEntityException;
import entidade.controller.JPA.exceptions.RollbackFailureException;
import entidade.persistencia.Compra;
import entidade.persistencia.FolhaPagamento;
import entidade.persistencia.Manutencao;
import entidade.persistencia.OperacaoExtraTipo;
import entidade.persistencia.OpercaoExtra;
import entidade.persistencia.Venda;
import java.util.List;
import javax.annotation.Resource;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceUnit;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.transaction.UserTransaction;

/**
 *
 * @author victorinox
 */
public class OpercaoExtraJpaController {
    @Resource
    private UserTransaction utx = null;
    @PersistenceUnit(unitName = "globalimage2PU")
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(OpercaoExtra opercaoExtra) throws RollbackFailureException, Exception {
        EntityManager em = null;
        try {
            utx.begin();
            em = getEntityManager();
            Venda vendaId = opercaoExtra.getVendaId();
            if (vendaId != null) {
                vendaId = em.getReference(vendaId.getClass(), vendaId.getVendaId());
                opercaoExtra.setVendaId(vendaId);
            }
            FolhaPagamento folhaPagamentoId = opercaoExtra.getFolhaPagamentoId();
            if (folhaPagamentoId != null) {
                folhaPagamentoId = em.getReference(folhaPagamentoId.getClass(), folhaPagamentoId.getFolhaPagamentoId());
                opercaoExtra.setFolhaPagamentoId(folhaPagamentoId);
            }
            Compra compraId = opercaoExtra.getCompraId();
            if (compraId != null) {
                compraId = em.getReference(compraId.getClass(), compraId.getCompraId());
                opercaoExtra.setCompraId(compraId);
            }
            Manutencao manutencaoId = opercaoExtra.getManutencaoId();
            if (manutencaoId != null) {
                manutencaoId = em.getReference(manutencaoId.getClass(), manutencaoId.getManutencaoId());
                opercaoExtra.setManutencaoId(manutencaoId);
            }
            OperacaoExtraTipo operacaoExtraTipoId = opercaoExtra.getOperacaoExtraTipoId();
            if (operacaoExtraTipoId != null) {
                operacaoExtraTipoId = em.getReference(operacaoExtraTipoId.getClass(), operacaoExtraTipoId.getOperacaoExtraTipoId());
                opercaoExtra.setOperacaoExtraTipoId(operacaoExtraTipoId);
            }
            em.persist(opercaoExtra);
            if (vendaId != null) {
                vendaId.getOpercaoExtraCollection().add(opercaoExtra);
                vendaId = em.merge(vendaId);
            }
            if (folhaPagamentoId != null) {
                folhaPagamentoId.getOpercaoExtraCollection().add(opercaoExtra);
                folhaPagamentoId = em.merge(folhaPagamentoId);
            }
            if (compraId != null) {
                compraId.getOpercaoExtraCollection().add(opercaoExtra);
                compraId = em.merge(compraId);
            }
            if (manutencaoId != null) {
                manutencaoId.getOpercaoExtraCollection().add(opercaoExtra);
                manutencaoId = em.merge(manutencaoId);
            }
            if (operacaoExtraTipoId != null) {
                operacaoExtraTipoId.getOpercaoExtraCollection().add(opercaoExtra);
                operacaoExtraTipoId = em.merge(operacaoExtraTipoId);
            }
            utx.commit();
        } catch (Exception ex) {
            try {
                utx.rollback();
            } catch (Exception re) {
                throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(OpercaoExtra opercaoExtra) throws NonexistentEntityException, RollbackFailureException, Exception {
        EntityManager em = null;
        try {
            utx.begin();
            em = getEntityManager();
            OpercaoExtra persistentOpercaoExtra = em.find(OpercaoExtra.class, opercaoExtra.getOperacaoExtraId());
            Venda vendaIdOld = persistentOpercaoExtra.getVendaId();
            Venda vendaIdNew = opercaoExtra.getVendaId();
            FolhaPagamento folhaPagamentoIdOld = persistentOpercaoExtra.getFolhaPagamentoId();
            FolhaPagamento folhaPagamentoIdNew = opercaoExtra.getFolhaPagamentoId();
            Compra compraIdOld = persistentOpercaoExtra.getCompraId();
            Compra compraIdNew = opercaoExtra.getCompraId();
            Manutencao manutencaoIdOld = persistentOpercaoExtra.getManutencaoId();
            Manutencao manutencaoIdNew = opercaoExtra.getManutencaoId();
            OperacaoExtraTipo operacaoExtraTipoIdOld = persistentOpercaoExtra.getOperacaoExtraTipoId();
            OperacaoExtraTipo operacaoExtraTipoIdNew = opercaoExtra.getOperacaoExtraTipoId();
            if (vendaIdNew != null) {
                vendaIdNew = em.getReference(vendaIdNew.getClass(), vendaIdNew.getVendaId());
                opercaoExtra.setVendaId(vendaIdNew);
            }
            if (folhaPagamentoIdNew != null) {
                folhaPagamentoIdNew = em.getReference(folhaPagamentoIdNew.getClass(), folhaPagamentoIdNew.getFolhaPagamentoId());
                opercaoExtra.setFolhaPagamentoId(folhaPagamentoIdNew);
            }
            if (compraIdNew != null) {
                compraIdNew = em.getReference(compraIdNew.getClass(), compraIdNew.getCompraId());
                opercaoExtra.setCompraId(compraIdNew);
            }
            if (manutencaoIdNew != null) {
                manutencaoIdNew = em.getReference(manutencaoIdNew.getClass(), manutencaoIdNew.getManutencaoId());
                opercaoExtra.setManutencaoId(manutencaoIdNew);
            }
            if (operacaoExtraTipoIdNew != null) {
                operacaoExtraTipoIdNew = em.getReference(operacaoExtraTipoIdNew.getClass(), operacaoExtraTipoIdNew.getOperacaoExtraTipoId());
                opercaoExtra.setOperacaoExtraTipoId(operacaoExtraTipoIdNew);
            }
            opercaoExtra = em.merge(opercaoExtra);
            if (vendaIdOld != null && !vendaIdOld.equals(vendaIdNew)) {
                vendaIdOld.getOpercaoExtraCollection().remove(opercaoExtra);
                vendaIdOld = em.merge(vendaIdOld);
            }
            if (vendaIdNew != null && !vendaIdNew.equals(vendaIdOld)) {
                vendaIdNew.getOpercaoExtraCollection().add(opercaoExtra);
                vendaIdNew = em.merge(vendaIdNew);
            }
            if (folhaPagamentoIdOld != null && !folhaPagamentoIdOld.equals(folhaPagamentoIdNew)) {
                folhaPagamentoIdOld.getOpercaoExtraCollection().remove(opercaoExtra);
                folhaPagamentoIdOld = em.merge(folhaPagamentoIdOld);
            }
            if (folhaPagamentoIdNew != null && !folhaPagamentoIdNew.equals(folhaPagamentoIdOld)) {
                folhaPagamentoIdNew.getOpercaoExtraCollection().add(opercaoExtra);
                folhaPagamentoIdNew = em.merge(folhaPagamentoIdNew);
            }
            if (compraIdOld != null && !compraIdOld.equals(compraIdNew)) {
                compraIdOld.getOpercaoExtraCollection().remove(opercaoExtra);
                compraIdOld = em.merge(compraIdOld);
            }
            if (compraIdNew != null && !compraIdNew.equals(compraIdOld)) {
                compraIdNew.getOpercaoExtraCollection().add(opercaoExtra);
                compraIdNew = em.merge(compraIdNew);
            }
            if (manutencaoIdOld != null && !manutencaoIdOld.equals(manutencaoIdNew)) {
                manutencaoIdOld.getOpercaoExtraCollection().remove(opercaoExtra);
                manutencaoIdOld = em.merge(manutencaoIdOld);
            }
            if (manutencaoIdNew != null && !manutencaoIdNew.equals(manutencaoIdOld)) {
                manutencaoIdNew.getOpercaoExtraCollection().add(opercaoExtra);
                manutencaoIdNew = em.merge(manutencaoIdNew);
            }
            if (operacaoExtraTipoIdOld != null && !operacaoExtraTipoIdOld.equals(operacaoExtraTipoIdNew)) {
                operacaoExtraTipoIdOld.getOpercaoExtraCollection().remove(opercaoExtra);
                operacaoExtraTipoIdOld = em.merge(operacaoExtraTipoIdOld);
            }
            if (operacaoExtraTipoIdNew != null && !operacaoExtraTipoIdNew.equals(operacaoExtraTipoIdOld)) {
                operacaoExtraTipoIdNew.getOpercaoExtraCollection().add(opercaoExtra);
                operacaoExtraTipoIdNew = em.merge(operacaoExtraTipoIdNew);
            }
            utx.commit();
        } catch (Exception ex) {
            try {
                utx.rollback();
            } catch (Exception re) {
                throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
            }
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = opercaoExtra.getOperacaoExtraId();
                if (findOpercaoExtra(id) == null) {
                    throw new NonexistentEntityException("The opercaoExtra with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws NonexistentEntityException, RollbackFailureException, Exception {
        EntityManager em = null;
        try {
            utx.begin();
            em = getEntityManager();
            OpercaoExtra opercaoExtra;
            try {
                opercaoExtra = em.getReference(OpercaoExtra.class, id);
                opercaoExtra.getOperacaoExtraId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The opercaoExtra with id " + id + " no longer exists.", enfe);
            }
            Venda vendaId = opercaoExtra.getVendaId();
            if (vendaId != null) {
                vendaId.getOpercaoExtraCollection().remove(opercaoExtra);
                vendaId = em.merge(vendaId);
            }
            FolhaPagamento folhaPagamentoId = opercaoExtra.getFolhaPagamentoId();
            if (folhaPagamentoId != null) {
                folhaPagamentoId.getOpercaoExtraCollection().remove(opercaoExtra);
                folhaPagamentoId = em.merge(folhaPagamentoId);
            }
            Compra compraId = opercaoExtra.getCompraId();
            if (compraId != null) {
                compraId.getOpercaoExtraCollection().remove(opercaoExtra);
                compraId = em.merge(compraId);
            }
            Manutencao manutencaoId = opercaoExtra.getManutencaoId();
            if (manutencaoId != null) {
                manutencaoId.getOpercaoExtraCollection().remove(opercaoExtra);
                manutencaoId = em.merge(manutencaoId);
            }
            OperacaoExtraTipo operacaoExtraTipoId = opercaoExtra.getOperacaoExtraTipoId();
            if (operacaoExtraTipoId != null) {
                operacaoExtraTipoId.getOpercaoExtraCollection().remove(opercaoExtra);
                operacaoExtraTipoId = em.merge(operacaoExtraTipoId);
            }
            em.remove(opercaoExtra);
            utx.commit();
        } catch (Exception ex) {
            try {
                utx.rollback();
            } catch (Exception re) {
                throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<OpercaoExtra> findOpercaoExtraEntities() {
        return findOpercaoExtraEntities(true, -1, -1);
    }

    public List<OpercaoExtra> findOpercaoExtraEntities(int maxResults, int firstResult) {
        return findOpercaoExtraEntities(false, maxResults, firstResult);
    }

    private List<OpercaoExtra> findOpercaoExtraEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select object(o) from OpercaoExtra as o");
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public OpercaoExtra findOpercaoExtra(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(OpercaoExtra.class, id);
        } finally {
            em.close();
        }
    }

    public int getOpercaoExtraCount() {
        EntityManager em = getEntityManager();
        try {
            return ((Long) em.createQuery("select count(o) from OpercaoExtra as o").getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }

}
